package com.zzh.bc.dao.project.impl;

import com.zzh.bc.dao.project.ProjectDao;
import com.zzh.bc.db.MySQLExecute;
import com.zzh.bc.db.RSUtils;
import com.zzh.bc.model.PageModel;
import com.zzh.bc.model.ProjectModel;
import com.zzh.bc.model.UserModel;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class ProjectDaoImpl implements ProjectDao {
    @Override
    public ProjectModel findObjById(String id) {
        return null;
    }

    @Override
    public List<ProjectModel> findAllObjByPage(PageModel pageModel) {
        return null;
    }

    @Override
    public List<ProjectModel> findObjByUserId(PageModel page, String userId) {
        String SQL = "SELECT " + ProjectModel.SQL_COLUMN_NAME + " FROM " + ProjectModel.TABLE_NAME + " WHERE created_by = ? ORDER BY updated_time DESC LIMIT ?, ?";
        List<ProjectModel> objects = null;
        try {
            objects = RSUtils.cast2ListBean(MySQLExecute.executeQuery(SQL, userId, page.getOffset(), page.getPageSize()), ProjectModel.class);

        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return objects;
    }

    @Override
    public boolean save(ProjectModel model) {
        String sql = "INSERT INTO " + ProjectModel.TABLE_NAME +
                " (" + ProjectModel.SQL_COLUMN_NAME + ") VALUES(" + ProjectModel.SQL_COLUMN_PARAMS + ")";
        try {
            return MySQLExecute.insert(sql, model.getProjectId(), model.getProjectName(), model.getCreateUserId(), model.getProjectDetails(), model.getProjectCover(), model.getProjectBaseUrl(), model.getCreatedBy(), model.getCreatedTime(), model.getUpdatedBy(), model.getUpdatedTime()) == 1;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean deleteProject(ProjectModel model) {
        String sql = "delete from " + ProjectModel.TABLE_NAME + " where project_id=? AND create_user_id=?";
        try {
            int delete = MySQLExecute.delete(sql, model.getProjectId(), model.getCreateUserId());
            return delete == 1;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return false;
    }

    @Override
    public ProjectModel findObjById(String... args) {
        String SQL = "SELECT " + ProjectModel.SQL_COLUMN_NAME + " FROM " + ProjectModel.TABLE_NAME + " WHERE created_by = ? AND project_id = ?";

        try {
            ResultSet executeQuery = MySQLExecute.executeQuery(SQL, args[0], args[1]);
            ProjectModel bean = RSUtils.cast2Bean(executeQuery, ProjectModel.class);
            return bean;
        } catch (SQLException sex) {
            sex.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
        return null;
    }
}
